<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small"  :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="项目编码"  prop="projectNo">
        <el-input v-model.trim="queryParams.projectNo" @change="handleQuery"  placeholder="请输入项目编码" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model.trim="queryParams.productName"  @change="handleQuery"  placeholder="请输入产品名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="PRD OA单号" prop="prdOaNo">
        <el-input v-model.trim="queryParams.prdOaNo" @change="handleQuery"   placeholder="请输入PRD OA单号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否超期" prop="overdue">
        <el-select v-model="queryParams.overdue" filterable  clearable  @change="handleQuery" >
          <el-option v-for="dict in dict.type.project_task_overdue" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="产品类型" prop="productType">
        <el-select v-model="queryParams.productType" filterable clearable  @change="handleQuery" >
          <el-option v-for="dict in dict.type.product_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="产品线" prop="productLine">
        <el-select v-model="queryParams.productLine" filterable clearable  @change="handleQuery" >
          <el-option v-for="dict in dict.type.product_line" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search"   @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh"   @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    
        <el-button type="primary" v-hasPermi="['projectManage:list:add']" plain  size="small" icon="el-icon-plus" @click="handleAdd()">创建项目</el-button>
      
        <el-table border v-loading="loading" :data="projectList" style="margin-top:10px">
          <el-table-column label="超期天数" :show-overflow-tooltip="true" align="center" prop="overdue"> 
            <template slot-scope="scope">
              <el-tag  type="danger"  v-if="scope.row.overdue > 0">  {{scope.row.overdue}} 天</el-tag>
              <el-tag  v-else>未超期</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="项目编码" align="left"   prop="projectNo" width="150">
            <template slot-scope="scope">
              <a style="color: cornflowerblue;" @click="openViewDetail(scope.row)">{{ scope.row.projectNo }}</a>
            </template>
          </el-table-column>
    
          <el-table-column label="项目名称" align="center" prop="projectName" width="150"/>
          <el-table-column label="产品名称" align="center" prop="productName" width="150"/>
          <el-table-column label="项目经理" align="center" prop="projectManagerName" width="120"/>
          <el-table-column label="MRD OA单号" align="center" prop="mrdOaNo" width="180">
            <template slot-scope="scope">
              <a style="color: cornflowerblue;"   @click="linkToOA(scope.row.mrdOaId)"> {{ scope.row.mrdOaNo }} <i class="el-icon-link"></i></a> 
            </template>
          </el-table-column>
          <el-table-column label="PRD OA单号" align="center" prop="prdOaNo" width="150">
            <template slot-scope="scope">
              <a style="color: cornflowerblue;"   @click="linkToOA(scope.row.prdOaId)"> {{ scope.row.prdOaNo }} <i class="el-icon-link"></i></a> 
            </template>
          </el-table-column>
          <el-table-column label="状态" :show-overflow-tooltip="true" align="center" prop="status"  width="150">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.project_list_status" :value="scope.row.status" />
            </template>
          </el-table-column>

          <el-table-column label="产品类型" :show-overflow-tooltip="true" align="center" prop="productType"  width="150">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.product_type" :value="scope.row.productType" />
            </template>
          </el-table-column>
          <el-table-column label="产品线" :show-overflow-tooltip="true" align="center" prop="productLine"  width="150">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.product_line" :value="scope.row.productLine" />
            </template>
          </el-table-column>

          <el-table-column label="创建时间" align="center" prop="createTime"  width="200">
            
          </el-table-column>
          <el-table-column label="创建人" align="center" prop="nickName" width="120"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
            <template slot-scope="scope">
              <el-button  size="mini"  type="text"    icon="el-icon-edit" @click="handleUpdate(scope.row)" v-show="scope.row.status <= 0" v-hasPermi="['projectManage:list:update']">提交</el-button>
              <el-button  size="mini"  type="text"    icon="el-icon-edit" @click="handleModify(scope.row)" v-show="scope.row.status > 0 && scope.row.status < 1000" v-hasPermi="['projectManage:list:update']">修改</el-button>
              <el-button  size="mini"  type="text"   icon="el-icon-delete"  @click="handleDel(scope.row)" v-show="scope.row.status == 0 " v-hasPermi="['projectManage:list:del']">删除</el-button>
              <el-button  size="mini"  type="text"   icon="el-icon-video-pause"  @click="handleStopProject(scope.row)" v-show="scope.row.status > 0 &&  scope.row.status < 1000"  v-hasPermi="['projectManage:list:stop']">中止</el-button>
              <el-button  size="mini"  type="text"   icon="el-icon-video-play"  @click="handleRestartProject(scope.row)" v-show="scope.row.status == 10000"  v-hasPermi="['projectManage:list:stop']">重启</el-button>
              <el-button size="small"  type="text" v-show="scope.row.status == 20"  icon="el-icon-close" @click="handleReject(scope.row)" v-hasPermi="['projectManage:list:settimemember']">驳回</el-button>
              <el-button  size="mini"  type="text"   icon="el-icon-s-tools" @click="handleSettime(scope.row)"  v-show="scope.row.status == 20 " v-hasPermi="['projectManage:list:settimemember']">设置时间</el-button>
              <el-button  size="mini"  type="text"   icon="el-icon-edit-outline" @click="handleModifyProject(scope.row)"  v-show="scope.row.status == 30 " v-hasPermi="['projectManage:project:modify']">项目变更</el-button>
              <el-button  size="mini"  type="text"   icon="el-icon-s-custom" @click="handleModifyManager(scope.row)"   v-hasPermi="['projectManage:project:modify']">变更项目经理</el-button>
              
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList" />
      
    
    

    <el-dialog :title="title" :visible="open" v-loading="dialogLoading" @close="closeOpen" width="1100px"  :close-on-click-modal="false" :close-on-press-escape="false"
          append-to-body>

          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>基本信息</span>
            </div>
            <div   class="text item">
              <el-form ref="form" :model="form" :inline="true" :rules="rules" label-width="110px">
                <el-form-item label="PRD OA单号" prop="prdOaNo" >
                  <el-input v-model="form.prdOaNo" placeholder="请输入PRD OA单号" :disabled="true"/> 
                </el-form-item>
                <el-tooltip class="item" v-if="!update" effect="dark" content="选择PRD" placement="top-start"> 
                  <i class="el-icon-search" style="margin-top: 8px;font-size:20px;cursor:pointer;" @click="handlePrd"></i>
                </el-tooltip>
                
                <el-form-item label="MRD OA单号" prop="mrdOaNo">
                  <el-input v-model="form.mrdOaNo" placeholder="请输入MRD OA单号" :disabled="true"/>
                </el-form-item>
                <el-form-item label="项目经理" prop="projectManagerName">
                  <el-input v-model="form.projectManagerName"  :disabled="true"/>
                </el-form-item>
                <el-form-item label="项目名称" prop="projectName">
                  <el-input v-model="form.projectName" placeholder="请输入项目名称" />
                </el-form-item>
                <el-form-item label="项目编号" prop="projectNo">
                  <el-input v-model="form.projectNo" placeholder="请输入项目编号" :disabled="update"/>
                </el-form-item>
                <el-form-item label="产品名称" prop="productName">
                  <el-input v-model="form.productName" placeholder="请输入产品名称" />
                </el-form-item>
                <el-form-item label="项目级别" prop="level">
                  <el-select v-model="form.level" filterable   clearable >
                    <el-option v-for="dict in dict.type.project_level" :key="dict.value" :label="dict.label" :value="dict.value" />
                  </el-select>
                </el-form-item>
                <el-form-item label="产品类型" prop="productType">
                  <el-select v-model="form.productType" filterable    clearable>
                    <el-option v-for="dict in dict.type.product_type" :key="dict.value" :label="dict.label" :value="dict.value" />
                  </el-select>
                </el-form-item>
                
                <el-form-item label="产品线" prop="productLine">
                  <el-select v-model="form.productLine" filterable   clearable >
                    <el-option v-for="dict in dict.type.product_line" :key="dict.value" :label="dict.label" :value="dict.value" />
                  </el-select>
                </el-form-item>
                <el-form-item label="OC型号" prop="ocModel">
                  <el-input v-model="form.ocModel" placeholder="请输入OC型号" />
                </el-form-item>
              </el-form>
            </div>
          </el-card>
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>项目描述</span>
            </div>
            <div   class="text item">
              <el-input type="textarea" v-model="form.remark"></el-input> 
            </div>
          </el-card>

          <el-card class="box-card" v-show="!modifyFlag">
            <div slot="header" class="clearfix">
              <span>项目成员</span>
              <el-button style="float: right; padding: 3px 0" @click="handelAddMember" type="text">添加成员</el-button>
            </div>
            <div   class="text item">
              <el-table  :data="projectMemberList" highlight-current-row style="width: 100%">
                <el-table-column type="index" label="行号" property="lineNo" > 
                </el-table-column>
                <el-table-column property="type" label="岗位">
                    <template slot-scope="scope">
                      <el-select v-model="scope.row.type" filterable clearable  style="width: 100%;"  >
                        <el-option v-for="dict in dict.type.owner_position" :key="dict.value" :disabled="dict.value == 'pjm'" :label="dict.label" :value="dict.value" />
                      </el-select>
                    </template>
                </el-table-column>
                <el-table-column property="userId" label="姓名">
                  <template slot-scope="scope">
                    <el-select v-model="scope.row.userId" filterable clearable  style="width: 100%;" >
                      <el-option v-for="user in userList" :key="user.userId" :label="user.nickName" :value="user.userId" />
                    </el-select> 
                  </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120">
                  <template slot-scope="scope">
                    <el-button size="mini" type="text"   icon="el-icon-delete" @click="handleDeleteMember(scope.row)" v-hasPermi="['projectManage:list:add']">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </el-card>

         
          <span slot="footer" class="dialog-footer" > 
            <el-button  @click="closeOpen">取 消</el-button>
            
            <el-button type="primary" v-show="!update" @click="onSure">提 交</el-button>
            <el-button type="primary" v-show="update" @click="onModify">提 交</el-button>
          </span>  
    </el-dialog>

    <el-dialog title="驳回项目" :visible="visible" width="500px"   @close="closeReject" >
      <el-form ref="form" :model="rejectData" label-width="80px" :rules="rejectRules">
        <el-form-item label="驳回原因" prop="remark">
          <el-input type="textarea" v-model="rejectData.remark"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onReject">提交</el-button>
          <el-button @click="closeReject">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    
    <settime ref="settime" @query="getList"  :title="title"></settime>
    <prd ref="prd"  @query="getList"   @ok="handleSelPrd"></prd>
    <modify ref="modify"   :title="title"></modify>
    <modify-manager ref="modifyManager"></modify-manager>
  </div>
</template>

<script>

import {getProjectList,addProject,getDetail,modifyProject,delProject,stopProject,draftProject,rejectProject,restartProject}  from "@/api/projectManage/projectList";
import {getTaskUsers,listUserAll,getUserProfile}  from "@/api/system/user";
import settime from "@/components/projectManage/templete/settime";
import modify from "@/components/projectManage/list/modifyProject";
import modifyManager  from "@/components/projectManage/list/modifyManager";
import prd from "@/components/oa/prd";
import {signLoginToEkp}  from "@/api/oa/oa";
import {query}  from "@/api/projectManage/projectManagerMember";

export default {
  name: "ProjectList",
  components: { settime,prd,modify,modifyManager },
  dicts: ['project_list_status','owner_position','project_task_overdue','project_level','product_line','product_type'],
  data() {
    return {

      //弹出层loading
      dialogLoading:false,

      title:null,

      titleMember:null,

      visible:false,

      update: false,
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //项目列表
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //项目成员列表
      projectMemberList:[],

      //人员列表
      userList:[], 

      //当前用户
      currUser:{},

      //驳回数据
      rejectData:{},

      //修改项目数据的标识
      modifyFlag:false,


      //行号
      lineNo:0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        myProjectFlag:1,
        projectNo: null,
        projectManager: null,
        mrdOaNo:null,
        prdOaNo:null,
        overdue:null,
        productType:null,
        productLine:null
      },
      form:{},
      param:{},
       
      rejectRules:{
          remark: [
            { required: true, message: "备注不能为空", trigger: "blur" }
          ], 
      },
      rules:{
          projectName: [
            { required: true, message: "项目名称不能为空", trigger: "blur" }
          ], 
          projectNo: [
            { required: true, message: "项目编号不能为空", trigger: "blur" }
          ], 
          level: [
            { required: true, message: "项目级别不能为空", trigger: "blur" }
          ], 
          mrdOaNo: [
            { required: true, message: "MRD OA单号不能为空", trigger: "blur" }
          ],
          prdOaNo: [
            { required: true, message: "PRD OA单号不能为空", trigger: "blur" }
          ],  
          productName: [
            { required: true, message: "产品名称不能为空", trigger: "blur" }
          ],
          productType: [
            { required: true, message: "产品类型不能为空", trigger: "blur" }
          ],
          productLine: [
            { required: true, message: "产品线不能为空", trigger: "blur" }
          ]
          
        },
        rulesMember:{
          type: [
            { required: true, message: "岗位不能为空", trigger: "blur" }
          ], 
          userId: [
            { required: true, message: "用户不能为空", trigger: "blur" }
          ], 
        }
    };
  },
  created() {
    this.getList();
    this.getCurrUserInfo();
    this.handleTaskUsers();
    // this.getTeamMember();
    
  },
 
  methods: {
 
    //驳回设置
    handleReject(obj){
      this.visible = true;
      this.rejectData.id = obj.id;
    },


    //提交
    onReject(){

      this.$refs["form"].validate((valid) => {
        if(valid){
          this.$modal.loading("请稍候...");
          rejectProject(this.rejectData).then(response=>{
            this.$modal.closeLoading();
            this.getList(); 
            this.closeReject();
          })
        }
      })


    },

    //驳回取消
    closeReject(){
      this.visible = false;
      this.rejectData = {};
    },


    getTeamMember(){
      query().then(response => {
        
        for(let i=0;i<response.data.length;i++){
            let item = response.data[i];
            item.lineNo = (i+1);
            this.projectMemberList.push(item);
        }  
 
         
      });
    },


    //查看OA表单详情
   linkToOA(oaId){
      signLoginToEkp(oaId).then(response=>{
        if(response.code == 200){
          window.open(response.data.authorizationURL, '_blank');
        }else{
          this.$alert(response.data.msg, '错误');
        }  
                   
      })
    },

    //项目变更
    handleModifyProject(row){ 
      this.$refs.modify.init("项目变更",row.id);  

    },

    handleModifyManager(row){
      this.$refs.modifyManager.open("项目经理变更",row); 
    },


    //选中PRD 流程
    handleSelPrd(record){

        this.form = record;
        this.form.projectManager = this.currUser.userId;
        this.form.projectManagerName = this.currUser.nickName;
 
 
    },


    //设置时间
    handleSettime(row){
        this.$refs.settime.open();  
        this.$refs.settime.init('设置项目时间',row.id);
    },


    //选择PRD
    handlePrd(){
        this.$refs.prd.open();  
    },



    //项目详情
    openViewDetail(row){
      this.$nextTick(() => {
        this.$router.push({
          path: "ProjectDetail",
          query: row
        })
      })

    },
 

    closeOpen() {
      this.open = false;
      this.modifyFlag = false;
      this.form={};
      this.update = false;
      this.projectMemberList=[];
    },


    //保存草稿
    onDraft(){
       this.$modal.loading("请稍候...");
       this.form.projectListMemberList = this.projectMemberList;
       draftProject(this.form).then(response => {
        this.getList();
        this.$modal.closeLoading();
        this.closeOpen(); 
      });
    },



    //加载当前用户信息
    getCurrUserInfo(){
       getUserProfile().then(response => {
           this.currUser = response.data;
           this.form.projectManager = this.currUser.userId;
           this.form.projectManagerName = this.currUser.nickName;
           
      });
    },


    //提交
    onSure(){ 
      this.$refs["form"].validate((valid) => {
        if(valid){ 
          for(let i=0;i<this.projectMemberList.length;i++){
            let item = this.projectMemberList[i]; 
            if(item.type == "" || item.userId == ""){
              this.$alert('第'+(i+1)+'行，岗位或姓名不能为空', '提示');
              return false;
            }
          }

           this.$modal.loading("请稍候...");
           this.form.projectListMemberList = this.projectMemberList;
            addProject(this.form).then(response => {
              this.getList();
              this.$modal.closeLoading();
              this.closeOpen(); 
            }).catch(err => { 
              this.$modal.closeLoading();
              this.$alert(err, '提示');
              
            });
        }
      })



    },

    //提交修改
    onModify(){
     
       this.$modal.loading("请稍候...");
       this.form.projectListMemberList = this.projectMemberList;
       //如果是修改草稿提交 则是直接提交项目
       if(this.form.status == 0 && this.update){
          this.form.status = 10;
       }
       this.form.modifyFlag = this.modifyFlag;
       modifyProject(this.form).then(response => {
        this.getList();
        this.$modal.closeLoading();
        this.closeOpen(); 
      });
    },




    //删除项目成员
    handleDeleteMember(row){
 
      let  projectMemberListTwo = [];
      this.projectMemberList = this.projectMemberList.filter(i=> i.lineNo !=row.lineNo );
 
      this.projectMemberList.forEach((item,index)=>{
            item.lineNo = (index+1);
            projectMemberListTwo.push(item);
      })
      this.projectMemberList = projectMemberListTwo;
    },
    //加载参加任务的人员列表
    handleTaskUsers(){

      getTaskUsers().then(response => {
        this.userList = response.data;
      });
    },
   
    //添加成员
    handelAddMember(){
      // this.titleMember = '添加成员';
      // this.visible= true;  
      let lineNoIndex = this.projectMemberList.length+1;
      let item = {
        lineNo:lineNoIndex, 
        type:'',
        userId:''

      };
      this.projectMemberList.push(item);
    },

    //关闭添加成员弹出框
    handelCloseMember(){
      this.visible= false;
      this.param= {};  
    },


    changeStatus(event) {
      this.queryParams.status = event
      this.getList()
    },
    /** 查询项目列表 */
    getList() {
      this.queryParams.exportFlag=null
      this.loading = true;
      getProjectList(this.queryParams).then(response => {
        this.projectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
 
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
 
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "创建项目";
      this.getTeamMember();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true;
      this.title = "修改项目数据";
      this.update = true;
      this.dialogLoading = true;
      listUserAll().then(response=>{
          this.userList = response.data;
          //获取项目详情
          getDetail(row.id).then(response =>{
              this.dialogLoading = false;
              this.form = response.data;
              this.projectMemberList = response.data.projectListMemberList;
          })
      })      
    },


    //纯修改项目数据 不涉及状态变更
    handleModify(row){
      this.open = true;
      this.title = "修改项目数据";
      this.update = true;
      this.modifyFlag = true;
      this.dialogLoading = true;
      listUserAll().then(response=>{
          this.userList = response.data;
          //获取项目详情
          getDetail(row.id).then(response =>{
              this.dialogLoading = false;
              this.form = response.data;
              this.projectMemberList = response.data.projectListMemberList;
          })
      })      
    },
 
    /** 删除按钮操作 */
    handleDel(row) {
       
      this.$modal.confirm('是否确认删除项目编号为"' + row.projectNo + '"的数据项？').then(function () {
        return delProject(row);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.queryParams.exportFlag="1"
      this.download('/business/project/export', {
        ...this.queryParams
      }, `${new Date().getTime()}_项目数据.xlsx`)
      
    },
    //项目中止
    handleStopProject(row) {
      
      this.$modal.confirm('是否确认中止项目编号为"' + row.projectNo + '"的数据项？').then(function () {
        return stopProject(row);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功!");
      }).catch(() => { });
       
    },

    //重启项目
    handleRestartProject(row){
        this.$modal.loading("请稍候...");
        let param = {id:row.id};
        restartProject(param).then(response=>{
          this.$modal.closeLoading();
          this.getList();
        })
    }
  }
};
</script>
<style scoped>
.el-col {
  height: 60px;
}

.labelword ::v-deep .el-form-item__label {
  white-space: pre-line !important;
  line-height: 15px !important;
}

.el-table .danger-row {
  background: rgb(187, 179, 184);
}

.el-table .success-row {
  background: #00FF00;
}
</style>
